#include "..\..\LunarLib\include\LunarWin32.h"
#include "..\..\LunarD3D\LunarD3D\MyD3D.h"


#define	DEFAULT_STYLE		LUNAR_WIN_TITLE | LUNAR_WIN_MAX | LUNAR_WIN_MIN | LUNAR_WIN_RESIZABLE
#define	DEFAULT_TITLE		"LUNATICJ APPLICATION"
#define	DEFAULT_WIDTH	640
#define	DEFAULT_HEIGHT	480

#pragma comment (lib, "LunarD3D.lib")

MyWin32*	pThis = NULL;
HINSTANCE	hInst;
LD3D*		pd3d = NULL;		

LRESULT	CALLBACK	WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_CREATE:
		//MessageBox(NULL, "tt", "tt", MB_OK);
		break;
	case WM_KEYDOWN:
		switch (wParam)
		{
		case VK_ESCAPE:
			//MessageBox(NULL, "tt1", "tt1", MB_OK);
			PostMessage(hWnd, WM_QUIT, 0, 0);
			break;
		}
		break;
	case WM_DESTROY:
		delete pThis;
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}


MyWin32::MyWin32()
{
	Title = DEFAULT_TITLE;
	Width = DEFAULT_WIDTH;
	Height = DEFAULT_HEIGHT;
	Style = DEFAULT_STYLE;
	hWnd = NULL;
	hInstance = NULL;

	pThis = this;
}

MyWin32::~MyWin32()
{
	if (hWnd)
	{
		UnregisterClass(Title, hInstance);
		DestroyWindow(hWnd);
	}
}

int MyWin32::Create(unsigned int style, int width, int height, char* title)
{
	WNDCLASS	wc;
	DWORD		winstyle;

	if (hWnd)
		return	WIN_ERROR;
	if (width)
		Width = width;
	if (height)
		Height = height;
	if (style)
		Style = style;

	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC)WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = (HINSTANCE)GetModuleHandle(NULL);
	wc.hIcon = NULL;
	wc.hCursor = NULL;
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName = title;
	wc.lpszClassName = title;

	if(RegisterClass(&wc) == 0)
		return WIN_ERROR;

	// make style
	winstyle = WS_OVERLAPPED|WS_SYSMENU;
	if(Style & LUNAR_WIN_TITLE)		// has caption
		winstyle |= WS_CAPTION;
	if(Style & LUNAR_WIN_MAX)			// has maximize box
		winstyle |= WS_MAXIMIZEBOX;
	if(Style & LUNAR_WIN_MIN)			// has minimize box
		winstyle |= WS_MINIMIZEBOX;
	if(Style & LUNAR_WIN_RESIZABLE)	// resizeable window
		winstyle |= WS_THICKFRAME;


	hWnd = CreateWindow(title, title, winstyle,0, 0, Width, Height, NULL, NULL, wc.hInstance, NULL);
	if(!hWnd)
		return WIN_ERROR;

	 ShowWindow( hWnd, SW_SHOWDEFAULT );
     UpdateWindow( hWnd );

	// ProcessMsg();

	return WIN_DONE;
}

HWND MyWin32::GetMyWinHandle()
{
	g_hWnd = hWnd;

	return g_hWnd;
}

int MyWin32::ProcessMsg(int apiOpt)
{
	MSG			msg;

	/*if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
	{ 
		if (msg.message == WM_QUIT)
			return WIN_ERROR;

		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}*/
	while (GetMessage(&msg, NULL, 0, 0))
	{
			
		TranslateMessage(&msg);
		DispatchMessage(&msg);

	}
	//	return WIN_DONE;
//	}

	return msg.wParam;
}

//int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
//{
//	pThis = new MyWin32();
//	pThis->Create(DEFAULT_STYLE, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_TITLE);
//}